package com.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;

import com.app.util.Utils_Log;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class G {
	private static String TAG = G.class.getSimpleName();
	public static boolean DEBUG = true;
	
	public static boolean bUtils_log_file = false; // log是否写入到本地文件


	public static String PACK_NAME = "";

	public static String PATH_DB ="";
	public static String PATH_LOG = "";
	public static String PATH_AUDIO = "";
	public static String PATH_VIDEO = "";
	public static String PATH_PIC = "";



	private static boolean INSIDE = false; // 是否保存在应用安装目录下
	private static boolean ENABLE_DIY_SYSTEM_BAR = true; // 默认开启沉浸式


	private static int color_system_bar = Color.TRANSPARENT;

	
	// 外部初始化入口
	public static void init( Context context, Map<String, Object> map) {

		try{
			if(map.size() > 0) {
				DEBUG = (Boolean) map.get("DEBUG");
				INSIDE = (Boolean) map.get("INSIDE");
				color_system_bar = (int)map.get("color_system_bar");
				ENABLE_DIY_SYSTEM_BAR = (Boolean)map.get("ENABLE_DIY_SYSTEM_BAR");
			}
		}
		catch (Exception e ){

		}


		try {
			initSavePath(context);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



		Utils_Log.e(TAG, "PATH_DB: "+PATH_DB);
		Utils_Log.e(TAG, "PATH_LOG: "+PATH_LOG);
		Utils_Log.e(TAG, "PATH_AUDIO: "+PATH_AUDIO);
		Utils_Log.e(TAG, "PATH_PIC: "+PATH_PIC);
		Utils_Log.e(TAG, "PATH_VIDEO: "+PATH_VIDEO);

	}
	
	
	private static void initSavePath(Context context) throws IOException {


		// 得到包名
		PACK_NAME = context.getPackageName();

		
		// 扩展储存是否存在
		boolean sdcardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		
		if( sdcardExist ){
			File sdCardpath = Environment.getExternalStorageDirectory();
			File dbPath = new File(sdCardpath, PACK_NAME);
			String str_dir = dbPath.getPath();
			if (INSIDE) // 保存在包内，这样卸载应用程序，内部的图片也会被删除; (inside话，拍照图片无法保存）
				str_dir = context.getCacheDir().getPath();


			File dir = new File(str_dir, "tmp/log"); // /mnt/sdcard/***/file
			if (!dir.exists())
				dir.mkdirs();
			File dir1 = new File(str_dir, "tmp/audio"); // /mnt/sdcard/***/file
			if (!dir1.exists())
				dir1.mkdirs();
			File dir2 = new File(str_dir, "tmp/db"); // /mnt/sdcard/***/file
			if (!dir2.exists())
				dir2.mkdirs();
			File dir3 = new File(str_dir, "tmp/pic"); // /mnt/sdcard/***/file
			if (!dir3.exists())
				dir3.mkdirs();
			File dir4 = new File(str_dir, "tmp/video"); // /mnt/sdcard/***/file
			if (!dir4.exists())
				dir4.mkdirs();



			// 保存路径
			PATH_LOG = dir.getAbsolutePath();
			PATH_AUDIO = dir1.getAbsolutePath();
			PATH_DB = dir2.getAbsolutePath();
			PATH_PIC = dir3.getAbsolutePath();
			PATH_VIDEO = dir4.getAbsolutePath();

		}
		else{
			File dir = new File(context.getCacheDir(), "tmp/log"); //
			if (!dir.exists())
				dir.mkdirs();
			File dir1 = new File(context.getCacheDir(), "tmp/audio"); //
			if (!dir1.exists())
				dir1.mkdirs();
			File dir2 = new File(context.getCacheDir(), "tmp/db"); // /mnt/sdcard/***/file
			if (!dir2.exists())
				dir2.mkdirs();
			File dir3 = new File(context.getCacheDir(), "tmp/pic"); // /mnt/sdcard/***/file
			if (!dir3.exists())
				dir3.mkdirs();
			File dir4 = new File(context.getCacheDir(), "tmp/video"); // /mnt/sdcard/***/file
			if (!dir4.exists())
				dir4.mkdirs();
			
			// 保存路径
			PATH_LOG = dir.getAbsolutePath();
			PATH_AUDIO = dir1.getAbsolutePath();
			PATH_DB = dir2.getAbsolutePath();
			PATH_PIC = dir3.getAbsolutePath();
			PATH_VIDEO = dir4.getAbsolutePath();

		}
		
	
		
		
		

	}






	public static void initSystemBar(Activity activity, boolean bUseDIY, int color) {
		if(!ENABLE_DIY_SYSTEM_BAR){
			return;
		}



		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			setTranslucentStatus(activity, true);

		}
		SystemBarTintManager tintManager = new SystemBarTintManager(activity);
		// enable status bar tint
		tintManager.setStatusBarTintEnabled(true);
		// enable navigation bar tint
		tintManager.setNavigationBarTintEnabled(true);
		// set a custom tint color for all system bars
		tintManager.setTintColor( bUseDIY?color:color_system_bar );


	}


	@TargetApi(19)
	private static void setTranslucentStatus(Activity activity, boolean on) {

		Window win = activity.getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

		if (on) {
			winParams.flags |= bits;

		} else {
			winParams.flags &= ~bits;


		}

		win.setAttributes(winParams);


	}






}
